package com.twg.smartUpload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.twg.util.UploadHelper;

public class SmartUploadServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SmartUploadServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置上传的路径
		String savePath = this.getServletContext().getRealPath(UploadHelper.PATH);
		File saveFile = new File(savePath);
		
		if(!saveFile.exists()){
			saveFile.mkdir();
		}
		Map<String,String> nameMap = new HashMap<String,String>();
		String message = "";
		try {
			//新建一个SmartUpload对象
			SmartUpload su = new SmartUpload();
			//初始化SmartUpload对象
			su.initialize(this.getServletConfig(),request,response);
			//设置单个文件的大小
			su.setMaxFileSize(1024*1024*10);
			//设置所以文件的大小
			su.setTotalMaxFileSize(1024*1024*100);
			//设置上传的文件类型
			su.setAllowedFilesList("jpg,png");
			//禁止上传的类型
			su.setDeniedFilesList("exe,jsp,html");
			//开始上传
			su.upload();
			//
			Files files = su.getFiles();
			for (int j = 0; j < files.getCount(); j++) {
				com.jspsmart.upload.File f = files.getFile(j);
				String fileName = f.getFileName().trim();
				fileName = UploadHelper.makeFileName(fileName);
				f.setFileName(fileName);
				System.out.println(fileName);
				System.out.println(f.getFieldName());
				System.out.println(f.getFileExt());
				System.out.println(f.getFilePathName());
				System.out.println(f.getSize());
				nameMap.put(f.getFieldName(), fileName);
			}
			//开始复制
			int i = su.save(savePath);
			System.out.println("上传成功文件数:" + i);
			message = "上传成功.";
			//获取非文件字段
			Request r = su.getRequest();
			Enumeration<String> names = r.getParameterNames();
			while(names.hasMoreElements()){
				String name = names.nextElement();
				String value = r.getParameter(name).trim();
				System.out.println(name);
				System.out.println(value);
				nameMap.put(name, value);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = "上传文件失败.";
			String ems = e.getMessage();
			if(ems.indexOf("1010") != -1){
				message = "上传文件失败:上传文件类型不正确！";
			}else if(ems.indexOf("1015") != -1){
				message = "上传文件失败:上传文件类型不正确！";
			}else if(ems.indexOf("1105") != -1){
				message = "上传文件失败:上传文件大小大于允许上传的最大值！";
			}else if(ems.indexOf("1015") != -1){
				message = "上传文件失败:上传文件的总大小大于我们允许上传总大小的最大值！";
			}
			e.printStackTrace();
		}
		
		request.setAttribute("message", message);
		request.getRequestDispatcher("../Result.jsp").forward(request, response);

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);

	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
